<?php

namespace App\Http\Controllers;

use App\Http\Resources\Comment;
use Illuminate\Http\Request;
use App\Models\BlogModel;
use Illuminate\Support\Facades\Auth;
use const http\Client\Curl\AUTH_ANY;
use App\Models\CommentModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Pagination\Paginator;

Paginator::useBootstrap();

class indexController extends Controller
{
    //用於首頁的展示，相關功能
    //首页文章列表获取
    public function article_list(){
        $user = Auth::guard('admin')->user();
        $data = BlogModel::paginate(5);
//        $data = DB::table('blog')->paginate(8);
//        $user = Auth::user('admin');
        $list = BlogModel::orderBy('views','desc')->take(8)->get();
        return view('index',compact('data','user','list'));

    }

//    点击阅读详情按钮
        public function detail(Request $request){
            //文章对象
            $data = BlogModel::find($request->id);
            //文章所有评论以及数量
            $commentdata = CommentModel::Where('blog_id',$request->id)->get();
            $commentnum = CommentModel::where('blog_id',$request->id)->get()->count();
            //当前登录用户对象
            $user = $request->user('admin');
            //浏览量+1
            BlogModel::Where('blog_id',$request->id)->increment('views',1);
            //热门文章
            $list = BlogModel::orderBy('views','desc')->take(8)->get();
            //传递的data与user都是数组，分别是文章详情数组，用户数组,还有评论
            return view('detail',compact('data','user','commentdata','commentnum','list'));
//dd($views);
        }

    //    点击编辑按钮
    public function detail2(Request $request){
        $data = BlogModel::find($request->id);
        $user = $request->user('admin');
        $list = BlogModel::orderBy('views','desc')->take(8)->get();
        return view('update',compact('data','user','list'));
    }


    //点击管理
    public function admin(){
        $data = BlogModel::orderBy('created_at','desc')->paginate(15);
        $user = Auth::guard('admin')->user();
//        dd(Auth::guard('admin')->id());
        $list = BlogModel::orderBy('views','desc')->take(8)->get();
        if (Auth::guard('admin')->id()==2){
            return view('admin',compact('data','user','list'));
        }else{
            echo '<script>alert(\'站长工具\');history.back();</script>';
        }
    }


    public function index(){
        $user = Auth::guard('admin')->user();
        $data = BlogModel::orderBy('created_at','desc')->paginate(5);
        $list = BlogModel::orderBy('views','desc')->take(8)->get();
//        $lable = DB::table('blog')->value('type');
//        dd($lable);
        return view('index',compact('user','data','list'));
    }

    //搜索
    public function  search(Request $request){
        $data = BlogModel::paginate(5);
//        $data = DB::table('blog')->paginate(8);
        $user = Auth::guard('admin')->user();
        $list = DB::table('blog')->where('title',$request['keyword'])
            ->orWhere('title','like','%'.$request['keyword'].'%')->get();
        return view('search',compact('data','user','list'));
    }



}
